package com.test.ita;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: gpss
 * Date: 8/3/11
 * Time: 10:34 PM
 * To change this template use File | Settings | File Templates.
 */
public class RebusGeneratorUI extends JPanel{
  public RebusGeneratorUI(ArrayList<String> rebusList) {
    // Use a FlowLayout layout manager. Left justify rows.
    // Leave 10 pixels of horizontal and vertical space between components.
    this.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));

    // Add some buttons to demonstrate the layout.
    String spaces = ""; // Used to make the buttons different
    for (String str : rebusList) {
        System.out.println("rebus : "+str);
      if (str != null && str.length()> 0 && str.startsWith(":")) {
          this.add(new ImagePanel(Utils.getImage(str).getImage()));
      } else {
          this.add(new JLabel(str));
      }
//      this.add(new JButton("Button #" + i + spaces));
//      spaces += " ";
    }

    // Give ourselves a default size
    this.setPreferredSize(new Dimension(500, 200));
  }

class ImagePanel extends JPanel {

  private Image img;

  public ImagePanel(String img) {
    this(new ImageIcon(img).getImage());
  }

  public ImagePanel(Image img) {
    this.img = img;
    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
    setPreferredSize(size);
    setMinimumSize(size);
    setMaximumSize(size);
    setSize(size);
    setLayout(null);
  }

  public void paintComponent(Graphics g) {
    g.drawImage(img, 0, 0, null);
  }

}

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    RebusGenerator rg = new RebusGenerator();
    ArrayList<String> rebusList = rg.generate(args);

    frame.getContentPane().add(new RebusGeneratorUI(rebusList), BorderLayout.CENTER);
    // Finally, set the size of the main window, and pop it up.
    frame.setSize(600, 400);
    frame.setVisible(true);
  }

}
